//图层
var exports = {};

var element = null;

var panel = $(".designerImages");


/*
 * 更新
 */
exports.update = function(){
	panel.empty();
	var face = Design.mainEditer.currFace();
	if(!face) return;
	var eles = face.elementList;
	for(var i=eles.length-1; i>=0; i--){
		var el = eles.getValueAt(i);
		if(el.type != "product" && el.type != "bg"){
			var dom = $("#layerInfo").html();
			dom = $(dom);
			var iconstr = el.getIcon();
			dom.find(".designerImage").html(iconstr);
			dom.attr("data-id", el.id);
			dom.attr("id", el.id);
			if(i == 0){
				dom.find(".designerImageNext").addClass("dis");
			}else if(i == eles.length-1){
				dom.find(".designerImagePre").addClass("dis");
			}
			panel.append(dom);
		}
	}
	
	panel.find(".designerImagePre").on("click", doHandler);
	panel.find(".designerImageNext").on("click", doHandler);
	panel.find(".designerImageView").on("click", doHandler);
	panel.find(".designerImageDel").on("click", doHandler);
	panel.find(".designerImageInfo").on("click", function(){
		var eleid = $(this).data("id");
		var ele = eles.getValue(eleid);
		if(ele) {
			ele.select();
		};
	});
}

function doHandler(e){
	var classname = $(this).attr("class");
	classname = $.trim(classname);
	var eleid = $(this).parents(".designerImageInfo").data("id");
	var eles = Design.mainEditer.currFace().elementList;
	var ele = eles.getValue(eleid);
	if(!ele) return;
	
	switch (classname){
		case "designerImagePre":
			ele.uplayer();
			ele.select();
			break;
		case "designerImageNext":
			ele.downlayer();
			ele.select();
			break;
		case "designerImageView":
			var icon = $(this).find(".icon-yincang");
			var isshow = icon.hasClass("icon-xianshi");
			if(isshow){
				ele.hide();
				icon.removeClass("icon-xianshi");
			}else{
				ele.show();
				icon.addClass("icon-xianshi");
				ele.select();
			}
			break;
		case "designerImageDel":
			Design.mainEditer.removeElement(eleid);
			break;
		default:
			break;
	}
	
	e.stopPropagation();
	e.preventDefault();
	return false;
}

/*
 * 选择
 */
exports.select = function($ele){
	var id = $ele.id;
	panel.find(".designerImageInfo").removeClass("active");
	panel.find("#"+id).addClass("active");
}

exports.unselect = function($ele){
	var id = $ele.id;
	panel.find("#"+id).removeClass("active");
}


module.exports = exports;